from lxml import etree, objectify

from xml.dom.minidom import parse
import xml.dom.minidom

from datetime import datetime
from datetime import timedelta

import requests
from requests_html import HTMLSession

import re

URL_MAIN = "http://172.16.8.65:8180/s/top/getNewItems?num={total}&keyword"
URL_T = "http://172.16.8.65:8180/s/content/download/t/{id}/view"
URL_P = "http://172.16.8.65:8180/s/content/download/p/{id}/view"
URL_CATOGORY = "http://172.16.8.65:8180/s/left/v_tree_new?fatherid=S"
# 三天内的新闻
URL_ALL = "http://172.16.8.65:8180/s/content/v_list_data"

def get_info(root, content):
    childs = root.getchildren()

    if len(childs) == 0:
        try:
            print(root.tag, root.text)
        except:
            print('error')
            print(root)
    else:
        for child in childs:
            get_info(child, content)


def method1():
    tree = etree.parse("test.xml")

    #print(tree.docinfo.root_name)

    root = tree.getroot()

    content = {}

    get_info(root, content)


def medthod2(article_type, article_id):
    body = None
    if article_type == 'T':
        # 纯文字
        res = requests.get(URL_T.format(id=article_id))
        if res.status_code == 200:
            body = res.content
    else:
        # 带图
        res = requests.get(URL_P.format(id=article_id))
        if res.status_code == 200:
            body = res.content

    DOMTree = xml.dom.minidom.parseString(body)
    collection = DOMTree.documentElement

    # 结果
    content = {}

    # 发送时间
    tmp = collection.getElementsByTagName("TransferTime")
    if tmp:
        content['created'] = tmp[0].childNodes[0].data
        content['created'] = (datetime.strptime(content['created'], '%Y-%m-%dT%H:%M:%S+08:00') + timedelta(
            hours=8)).strftime('%Y-%m-%d %H:%M:%S')
    else:
        content['created'] = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
    #来源
    content['from'] =  collection.getElementsByTagName("NameTopic")[0].getElementsByTagName('Name')[0].childNodes[0].data
    content['from'] = content['from'].strip()

    #编辑
    tmp = collection.getElementsByTagName("FullName")
    if tmp:
        content['editor'] = collection.getElementsByTagName("FullName")[0].childNodes[0].data
        content['editor'] = content['editor'].strip()
    else:
        content['editor'] = "未知"
    #类型
    content['subjects'] = [e.childNodes[0].data.strip() for e in collection.getElementsByTagName(
        "SubjectCodes")[0].getElementsByTagName('Name')]

    #关键词
    content['keywords'] = collection.getElementsByTagName("Keyword")[0].childNodes[0].data.strip()
    #用队列，“,”分割
    content['keywords'] = content['keywords'].split(',')

    #事情发生地点
    tmp = collection.getElementsByTagName("Location")
    if tmp:
        content['happen_place'] = tmp[0].getElementsByTagName(
            'Name')[0].childNodes[0].data
    else:
        content['happen_place'] = "未知"

    #内容
    tmp = collection.getElementsByTagName("WordCount")
    if tmp:
        try:
            content['wordcount'] = collection.getElementsByTagName("WordCount")[0].childNodes[0].data
            if content['wordcount'] == '0':
                content['data_content'] = "暂无内容"
            else:
                content['data_content'] = collection.getElementsByTagName("DataContent")[0].childNodes[0].data
        except:
            content['data_content'] = "暂无内容"
            content['wordcount'] = '0'
    else:
        tmp = collection.getElementsByTagName("DataContent")
        if tmp:
            content['data_content'] = collection.getElementsByTagName("DataContent")[0].childNodes[0].data
            content['wordcount'] = len(content['data_content'])
        else:
            content['data_content'] = "暂无内容"


    return content


def get_article_list(total):
    res = requests.get(URL_MAIN.format(total=total))
    res = res.json()

    acticles = []
    for item in res:
        if item['articleType'] == 'T':
            try:

                tmp_content = medthod2(item['articleType'], item['id'])
                tmp_content['title'] = item['title']
                tmp_content['article_id'] = item['id']
                acticles.append(tmp_content)
            except Exception as e:
                print(item['articleType'], item['id'], item['title'])

    return acticles




def get_t_article(startDate, endDate, total=3,category='',title='',keyword=''):
    articleType = 'T'
    session = HTMLSession()
    data = {
        'title': title,
        'keyword': keyword,
        'tid': 't',
        'articleType': '',
        'startDate': startDate,
        'endDate': endDate,
        'pid': '',
        'iinational': '',
        'category': category,
        'pageSize': total,
        'pageNo': 1,
        'showType': 'pic',
        'articleId': '',
    }
    r = session.post(URL_ALL, data=data)
    res = r.html.find('.itemTitle')

    acticles = []
    for item in res:
        if item.attrs.get('href'):
            item_id =  item.attrs.get('href').split('.')[0].split('/')[-1]
        else:
            print(item.text, item.attrs['href'])
            continue
        try:
            tmp_content = medthod2(articleType, item_id)
            tmp_content['title'] = item.text
            tmp_content['article_id'] = item_id
            acticles.append(tmp_content)
        except Exception as e:
            print(e)
            print(item_id, item.text)


    return acticles


def get_p_article(startDate, total=10):
    session = HTMLSession()

    r = session.get(URL_ALL + '?tid=p&pid=&iinational=&category=&startDate={startDate}&pageSize={pageSize}'.format(
        startDate=startDate,pageSize=total))
    res = r.html.find('.col1')

    acticles = {}
    for item in res:

        img_div = item.find('.imglink', first=True)
        title = item.find('.itemTitle', first=True).text
        item_id = item.find('.title', first=True).attrs.get('href').split('.')[0].split('/')[-1]
        title = re.sub(r'（\d*）', '', title)


        if not acticles.get(title):
            acticles[title] = {'item_id': item_id, 'pics':[]}

        acticles[title]['pics'].append('http://172.16.8.65:8180' + img_div.attrs.get('href'))

    for title, content in acticles.items():
        tmp_content = medthod2('P', content['item_id'])
        content.update(tmp_content)

    return acticles

def get_category_list():
    res = requests.get(URL_CATOGORY)
    res = res.json()
    result = []
    for item in res:
        if item['pId'] == 't':
            item['id'] = item['id'][1:]
            result.append(item)
    return result

if __name__ == '__main__':
    #res = get_article_list(50)
    #print(res[30:])
    print(get_p_article('2019-08-05'))
    # print(get_t_article('2019-03-04', '2019-03-04', category='12'))